package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Customer;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.SystemDictionaryItem;
import cn.wolfcode.query.CustomerQueryObject;
import cn.wolfcode.service.ICustomerService;
import cn.wolfcode.query.QueryObject;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.ISystemDictionaryItemService;
import cn.wolfcode.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private ICustomerService customerService;
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;


    @RequiresPermissions(value = {"customer:potentialList", "潜在客户列表"}, logical = Logical.OR)
    @RequestMapping("/potentialList")
    public String potentialList (Model model, @ModelAttribute("qo") CustomerQueryObject qo) {
        //只查询潜在客户状态
        qo.setStatus(Customer.STATUS_COMMON);

        //判断登录的员工销售专员角色只能查看自己的客户,管理员或者销售经理可以看到所有客户
        Subject subject = SecurityUtils.getSubject();
        if (!(subject.hasRole("ADMIN") || subject.hasRole("Market_Manager"))) {
            Employee employee = (Employee) subject.getPrincipal();
            qo.setSellerId(employee.getId());
        }

        PageInfo<Customer> pageInfo = customerService.query(qo);
        model.addAttribute("pageInfo", pageInfo);

        //查询到拥有以下两个角色的员工
        List<Employee> sellers = employeeService.selectByRoleSn("Market", "Market_Manager");
        //共享到页面查询下拉框
        model.addAttribute("sellers", sellers);

        //职业下拉框数据
        List<SystemDictionaryItem> jobs = systemDictionaryItemService.selectByPrentSn("job");
        model.addAttribute("jobs", jobs);

        //来源下拉框数据
        List<SystemDictionaryItem> sources = systemDictionaryItemService.selectByPrentSn("source");
        model.addAttribute("sources", sources);

        //交流方式下拉框数据
        List<SystemDictionaryItem> ccts = systemDictionaryItemService.selectByPrentSn("communicationMethod");
        model.addAttribute("ccts", ccts);

        return "customer/potentialList";
    }

    @RequiresPermissions(value = {"customer:poolList", "客户池列表"}, logical = Logical.OR)
    @RequestMapping("/poolList")
    public String poolList (Model model, @ModelAttribute("qo") CustomerQueryObject qo) {
        //只查询客户池状态
        qo.setStatus(Customer.STATUS_POOL);

        //判断登录的员工销售专员角色只能查看自己的客户,管理员或者销售经理可以看到所有客户
        Subject subject = SecurityUtils.getSubject();
        if (!(subject.hasRole("ADMIN") || subject.hasRole("Market_Manager"))) {
            Employee employee = (Employee) subject.getPrincipal();
            qo.setSellerId(employee.getId());
        }

        PageInfo<Customer> pageInfo = customerService.query(qo);
        model.addAttribute("pageInfo", pageInfo);

        //查询到拥有以下两个角色的员工
        List<Employee> sellers = employeeService.selectByRoleSn("Market", "Market_Manager");
        //共享到页面查询下拉框
        model.addAttribute("sellers", sellers);

        List<SystemDictionaryItem> jobs = systemDictionaryItemService.selectByPrentSn("job");
        model.addAttribute("jobs", jobs);

        List<SystemDictionaryItem> sources = systemDictionaryItemService.selectByPrentSn("source");
        model.addAttribute("sources", sources);


        return "customer/poolList";
    }


    @RequestMapping("/changeStatus")
    @ResponseBody
    public JsonResult changeStatus (Customer customer) {
        customerService.changeStatus(customer);
        return new JsonResult();
    }


    @RequestMapping("/delete")
    @RequiresPermissions(value = {"customer:delete", "客户删除"}, logical = Logical.OR)
    @ResponseBody
    public JsonResult delete (Long id) {
        if (id != null) {
            customerService.delete(id);
        }
        return new JsonResult();
    }


    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"customer:saveOrUpdate", "客户新增/编辑"}, logical = Logical.OR)
    @ResponseBody
    public JsonResult saveOrUpdate (Customer customer) {
        Customer selectByTel = customerService.selectByTel(customer.getTel());
        if (customer.getId() != null) {
            if (selectByTel == null || selectByTel.getTel().equals(customer.getTel())) {
                customerService.update(customer);
                return new JsonResult();
            }
            return new JsonResult(false, "该客户已存在");
        } else {
            if (selectByTel != null) {
                return new JsonResult();
            }
            customerService.save(customer);
            return new JsonResult();
        }
    }


    //验证用户名
    @RequestMapping("/checkTel")
    @ResponseBody
    public HashMap checkTel (String tel, Long id) {
        HashMap<Object, Object> map = new HashMap<>();
        Customer customer = customerService.selectByTel(tel);
        Customer get = customerService.get(id);
        if (id != null) {
            if (get.getTel().equals(tel)) {
                map.put("valid", true);
                return map;
            } else {
                map.put("valid", customer == null);
                return map;
            }
        } else {
            map.put("valid", customer == null);
            return map;
        }
    }
}
